# -*- coding: utf-8 -*-
"""生成默认头像占位图"""
from PIL import Image, ImageDraw
import os

SIZE = 200  # 头像尺寸（正方形）
BG_COLOR = (245, 245, 245)  # 背景色 #f5f5f5
ICON_COLOR = (153, 153, 153)  # 图标颜色 #999999

def generate_avatar():
    """生成默认头像"""
    # 创建图片（透明背景）
    img = Image.new('RGBA', (SIZE, SIZE), (255, 255, 255, 0))
    draw = ImageDraw.Draw(img)
    
    # 绘制圆形背景
    padding = 10
    circle_box = [padding, padding, SIZE - padding, SIZE - padding]
    draw.ellipse(circle_box, fill=(220, 220, 220, 255), outline=(200, 200, 200, 255), width=2)
    
    # 绘制用户图标（简化的头像）
    center_x, center_y = SIZE // 2, SIZE // 2
    
    # 头部（圆形）
    head_radius = SIZE // 6
    head_box = [
        center_x - head_radius,
        center_y - head_radius - 10,
        center_x + head_radius,
        center_y - head_radius + 10
    ]
    draw.ellipse(head_box, fill=ICON_COLOR + (255,))
    
    # 身体（椭圆形）
    body_box = [
        center_x - int(head_radius * 1.2),
        center_y - head_radius + 10,
        center_x + int(head_radius * 1.2),
        center_y + int(head_radius * 1.5)
    ]
    draw.ellipse(body_box, fill=ICON_COLOR + (255,))
    
    # 保存图片
    script_dir = os.path.dirname(os.path.abspath(__file__))
    project_root = os.path.dirname(script_dir)
    output_path = os.path.join(project_root, 'src', 'static', 'default-avatar.png')
    
    # 确保目录存在
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    
    img.save(output_path, 'PNG')
    print('✓ 默认头像已生成: {}'.format(output_path))
    print('  尺寸: {}x{}px'.format(SIZE, SIZE))

if __name__ == '__main__':
    try:
        generate_avatar()
    except ImportError:
        print('请先安装 Pillow: pip install Pillow')
    except Exception as e:
        print('错误: {}'.format(e))
        import traceback
        traceback.print_exc()

